package com.welkom.everydate.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.welkom.everydate.common.Constants;
import com.welkom.everydate.domain.Authority;
import com.welkom.everydate.services.interfaces.AuthorityService;
import com.welkom.everydate.services.interfaces.ResourceService;
import com.welkom.everydate.util.MessageReader;
import com.welkom.everydate.util.web.Operation;
import com.welkom.everydate.util.web.PageData;
import com.welkom.everydate.util.web.PageHeaderAnnotationResolver;
import com.welkom.everydate.util.web.RequestResult;
@Controller
public class AuthorityManagement {
	@Resource
	AuthorityService authorityService;
	@Resource
	ResourceService resourceService;
	@RequestMapping("/authorityManagement.html")
	public String resourceManagement(Model model){
		return "system/authorityManagement";
	}
	/**
	 * 查询权限列表
	 * @param authorityJsonString
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/authorityQuery.html")
	@ResponseBody
	public PageData<Authority> queryAuthoritiesByPage(@RequestParam(required=true,value="authorityJsonString") String authorityJsonString,@RequestParam(required=true,value="pageNum") int pageNum,@RequestParam(required=true,value="pageSize") int pageSize){
		 JSONArray array = JSONArray.fromObject(authorityJsonString);//先读取串数组
		 Object[] objectArray = array.toArray();  
		 while(objectArray[0] instanceof JSONArray){
			 JSONArray currentArray=(JSONArray)objectArray[0];
			 Object[] currentObjectArray=currentArray.toArray();
			 objectArray=currentObjectArray;
		 }
		 if(pageSize==0){
				pageSize=Constants.Pagination.DEFAULT_PAGE_SIZE;
		 }
		 if(pageNum==0){
			 pageNum=1;
		 }
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 Authority authority=(Authority)obj.toBean(obj, Authority.class);
		 int totalCount=authorityService.getAuthorityTotalCountByCondition(authority);
		 PageData<Authority> pageData=new PageData<Authority>();
		 pageData.setCurrentPage(pageNum);
		 pageData.setCurrentPageSize(pageSize);
		 pageData.setTotalCount(totalCount);
		 if(pageData.getPageNavigatorLength()==0){
			 pageData.setPageNavigatorLength(Constants.Pagination.DEFAULT_PAGE_NAVIGATOR_LENGTH);
		 }
		 pageData.setPageHeaders(PageHeaderAnnotationResolver.getHeadersFromDomain(Authority.class));
		 List<Operation> operations=new ArrayList<Operation>();
		 pageData.setOperations(operations);
		 List<Authority> authorities=authorityService.queryAuthoritiesByPage(authority, pageData.getStartIndex(), pageSize);
		 pageData.setData(authorities);
		 return pageData;
	}
	/**
	 * 获取权限列表的详细信息
	 * @param authorityId
	 * @return
	 */
	@RequestMapping("/getAuthorityDetail.html")
	@ResponseBody
	public RequestResult<Authority> getAuthorityDetail(@RequestParam(required=true,value="authorityId") long authorityId){
		RequestResult<Authority> result=new RequestResult<Authority>();
		Authority authority=authorityService.getAuthorityById(authorityId);
		if(authority==null){
			 result.setResultCode(Constants.ResultCode.RESULT_CODE_FAIL);
			 result.setReason(MessageReader.getMessage(Constants.Message.MESSAGE_OBJECT_NOT_EXSIT));
		}else{
			 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
			 result.setData(authority);
		 }
		return result;
	}
	/**
	 * 保存对权限的修改
	 * @param authorityJsonString
	 * @return
	 */
	@RequestMapping("/saveChangesToAuthority.html")
	@ResponseBody
	public RequestResult<Authority> saveChangesToAuthority(@RequestParam(required=true,value="authorityJsonString")String authorityJsonString){
		JSONArray array = JSONArray.fromObject(authorityJsonString);//先读取串数组
		 Object[] objectArray = array.toArray();  
		 while(objectArray[0] instanceof JSONArray){
			 JSONArray currentArray=(JSONArray)objectArray[0];
			 Object[] currentObjectArray=currentArray.toArray();
			 objectArray=currentObjectArray;
		 }
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 Authority authority=(Authority)obj.toBean(obj, Authority.class);
		 authorityService.saveChangesToAuthority(authority);
		 RequestResult<Authority> result=new RequestResult<Authority>();
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 result.setData(authority);
		 return result;
	}
	/**
	 * 新增权限
	 * @param authorityJsonString
	 * @return
	 */
	@RequestMapping("/addAuthority.html")
	@ResponseBody
	public RequestResult<Authority> addAuthority(@RequestParam(required=true,value="authorityJsonString")String authorityJsonString){
		JSONArray array = JSONArray.fromObject(authorityJsonString);//先读取串数组
		 Object[] objectArray = array.toArray();  
		 while(objectArray[0] instanceof JSONArray){
			 JSONArray currentArray=(JSONArray)objectArray[0];
			 Object[] currentObjectArray=currentArray.toArray();
			 objectArray=currentObjectArray;
		 }
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 Authority authority=(Authority)obj.toBean(obj, Authority.class);
		 authorityService.addAuthority(authority);
		 RequestResult<Authority> result=new RequestResult<Authority>();
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 result.setData(authority);
		 return result;
	}
	
	
	@RequestMapping("/removeAuthorityById.html")
	@ResponseBody
	public RequestResult<Authority> removeResultById(@RequestParam(required=true,value="authorityId")long authorityId){
		authorityService.removeAuthorityById(authorityId);
		RequestResult<Authority> result=new RequestResult<Authority>();
		result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		return result;
	}
	
	@RequestMapping("/saveResourceChangesToAuthority.html")
	@ResponseBody
	public RequestResult<Authority> saveResourceChangesToAuthority(@RequestParam(required=true,value="resourceJsonString")String resourceJsonString ,@RequestParam(required=true,value="authorityId")long authorityId){
		JSONArray array = JSONArray.fromObject(resourceJsonString);//先读取串数组
		Object[] objectArray = array.toArray();  
		List<com.welkom.everydate.domain.Resource> resources=new ArrayList<com.welkom.everydate.domain.Resource>();
		for(int i=0;i<objectArray.length;i++){
			JSONObject obj = JSONObject.fromObject(objectArray[i]);
			com.welkom.everydate.domain.Resource resource=(com.welkom.everydate.domain.Resource)obj.toBean(obj, com.welkom.everydate.domain.Resource.class);
			resources.add(resource);
		}
		resourceService.removeAuthorityResourceRelationsByAuthorityId(authorityId);
		resourceService.addAuthorityResourceRelations(resources, authorityId);
		RequestResult<Authority> result=new RequestResult<Authority>();
		result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		return result;
	}
	
	@RequestMapping("/getCurrentAuthorityResources.html")
	@ResponseBody
	public RequestResult<Map> getCurrentAuthorityResources(@RequestParam(required=true,value="authorityId")long authorityId){
		RequestResult<Map> result=new RequestResult<Map>();
		List<com.welkom.everydate.domain.Resource> allResources=resourceService.getAllResources();
		List<com.welkom.everydate.domain.Resource> currentResources=resourceService.getResourcesByAuthorityId(authorityId);
		List<com.welkom.everydate.domain.Resource> otherResources=new ArrayList<com.welkom.everydate.domain.Resource>();
		for(com.welkom.everydate.domain.Resource resource:allResources){
			if(!currentResources.contains(resource)){
				otherResources.add(resource);
			}
		}
		Map<String,Object> data=new HashMap<String,Object>();
		data.put("currentResources", currentResources);
		data.put("otherResources", otherResources);
		result.setData(data);
		result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		return result;
	}

}
